package org.jxc.service;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.hibernate.SessionFactory;
import org.jxc.constants.JXCString;
import org.jxc.dao.ArriveOrderDAO;
import org.jxc.dao.ArriveOrderProductDAO;
import org.jxc.dao.InOrderDAO;
import org.jxc.dao.InOrderProductDAO;
import org.jxc.dao.SalesBackOrderDAO;
import org.jxc.dao.SalesBackOrderProductDAO;
import org.jxc.domain.ArriveOrder;
import org.jxc.domain.ArriveOrderProduct;
import org.jxc.domain.InOrder;
import org.jxc.domain.InOrderProduct;
import org.jxc.domain.SalesBackOrder;
import org.jxc.domain.SalesBackOrderProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.team4.dao.DAOFactory;
import org.team4.domain.RoleUsers;
import org.team4.domain.Users;
import org.team4.exception.CRUDException;
import org.team4.service.IService;
import org.team4.util.LoginBean;
import org.team4.util.Validate;
import org.team4.vo.Page;

@Service
public class InOrderService implements IService {

	private InOrderDAO inOrderDAO;
	private InOrderProductDAO inOrderProductDAO;
	private ArriveOrderDAO arriveOrderDAO;
	private ArriveOrderProductDAO arriveOrderProductDAO;
	private SalesBackOrderDAO salesBackOrderDAO;
	private SalesBackOrderProductDAO salesBackOrderProductDAO;

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		inOrderDAO = (InOrderDAO) DAOFactory.getDAO(sessionFactory,
				InOrderDAO.class);
		inOrderProductDAO = (InOrderProductDAO) DAOFactory.getDAO(
				sessionFactory, InOrderProductDAO.class);
		arriveOrderDAO = (ArriveOrderDAO) DAOFactory.getDAO(sessionFactory,
				ArriveOrderDAO.class);
		arriveOrderProductDAO = (ArriveOrderProductDAO) DAOFactory.getDAO(
				sessionFactory, ArriveOrderProductDAO.class);
		salesBackOrderDAO = (SalesBackOrderDAO) DAOFactory.getDAO(
				sessionFactory, SalesBackOrderDAO.class);
		salesBackOrderProductDAO = (SalesBackOrderProductDAO) DAOFactory
				.getDAO(sessionFactory, SalesBackOrderProductDAO.class);
	}

	public String saveForArrive(String arriveOrderId) throws CRUDException {
		List<InOrder> ios = inOrderDAO.find(
				"from InOrder where arriveOrder.id=?", arriveOrderId);
		if (Validate.collectionNotNull(ios)) {
			return ios.get(0).getId();
		} else {
			ArriveOrder ao = arriveOrderDAO.get(arriveOrderId);
			InOrder io = new InOrder();
			io.setArriveOrder(ao);
			io.setSalesBackOrder(null);
			io.setOperator(LoginBean.getLoginBean().getUser());
			io.setInStockDate(new Date());
			io.setCreator(LoginBean.getLoginBean().getUser());
			io.setCreateTime(new Date());
			io.setAuditor(LoginBean.getLoginBean().getUser().getDepartment()
					.getManager());
			io.setAuditState(JXCString.NOTAPPLY);
			List<ArriveOrderProduct> aopList = arriveOrderProductDAO.find(
					"from ArriveOrderProduct where arriveOrder.id=?",
					ao.getId());
			int totalCount = 0;
			for (ArriveOrderProduct aop : aopList) {
				totalCount += aop.getInAmounts();
			}
			io.setTotalCount(totalCount);
			io.setDeleteFlag("0");
			io.setIsBack(0);
			inOrderDAO.save(io);

			for (ArriveOrderProduct aop : aopList) {
				InOrderProduct iop = new InOrderProduct();
				iop.setArriveAmounts(aop.getInAmounts());
				iop.setInAmounts(aop.getInAmounts());
				iop.setInPrice(aop.getPrice());
				iop.setTotalPrice(aop.getInAmounts() * aop.getPrice());
				iop.setProduct(aop.getProduct());
				iop.setInOrder(io);
				iop.setArriveOrderProduct(aop);
				inOrderProductDAO.save(iop);
			}
			ao.setEditor(LoginBean.getLoginBean().getUser());
			ao.setEditTime(new Date());
			arriveOrderDAO.update(ao);
			return io.getId();
		}
	}

	public String saveForBack(String salesBackOrderId) throws CRUDException {
		List<InOrder> ios = inOrderDAO.find(
				"from InOrder where salesBackOrder.id=?", salesBackOrderId);
		if (Validate.collectionNotNull(ios)) {
			return ios.get(0).getId();
		} else {
			SalesBackOrder sbo = salesBackOrderDAO.get(salesBackOrderId);
			InOrder io = new InOrder();
			io.setArriveOrder(null);
			io.setSalesBackOrder(sbo);
			io.setOperator(LoginBean.getLoginBean().getUser());
			io.setInStockDate(new Date());
			io.setCreator(LoginBean.getLoginBean().getUser());
			io.setCreateTime(new Date());
			io.setAuditor(LoginBean.getLoginBean().getUser().getDepartment()
					.getManager());
			io.setAuditState(JXCString.NOTAPPLY);
			List<SalesBackOrderProduct> sbopList = salesBackOrderProductDAO
					.find("from SalesBackOrderProduct where salesBackOrder.id=?",
							sbo.getId());
			int totalCount = 0;
			for (SalesBackOrderProduct sbop : sbopList) {
				totalCount += sbop.getAmounts();
			}
			io.setTotalCount(totalCount);
			io.setDeleteFlag("0");
			io.setIsBack(1);
			inOrderDAO.save(io);

			for (SalesBackOrderProduct sbop : sbopList) {
				InOrderProduct iop = new InOrderProduct();
				iop.setInAmounts(sbop.getAmounts());
				iop.setInPrice(sbop.getSalesOrderProduct().getPrice());
				iop.setTotalPrice(sbop.getAmounts()
						* sbop.getSalesOrderProduct().getPrice());
				iop.setProduct(sbop.getSalesOrderProduct().getProduct());
				iop.setInOrder(io);
				iop.setSalesBackOrderProduct(sbop);
				inOrderProductDAO.save(iop);
			}
			sbo.setEditor(LoginBean.getLoginBean().getUser());
			sbo.setEditTime(new Date());
			salesBackOrderDAO.update(sbo);
			return io.getId();
		}
	}

	public InOrder findInOrderByArriveOrderId(String arriveOrderId)
			throws CRUDException {
		return (InOrder) inOrderDAO.findUnique(
				"from InOrder where arriveOrder.id = ?", arriveOrderId);
	}

	public InOrder findInOrderBySalesBackOrderId(String salesBackOrderId)
			throws CRUDException {
		return (InOrder) inOrderDAO.findUnique(
				"from InOrder where salesBackOrder.id = ?", salesBackOrderId);
	}

	public void update(InOrder io) throws CRUDException {
		InOrder old = inOrderDAO.get(io.getId());
		old.setCode(io.getCode());
		old.setAuditState(io.getAuditState());
		// old.setArriveOrder(io.getArriveOrder());
		// old.setSalesBackOrder(io.getSalesBackOrder());
		old.setTotalCount(io.getTotalCount());
		old.setOperator(LoginBean.getLoginBean().getUser());
		old.setInStockDate(io.getInStockDate());
		old.setEditor(LoginBean.getLoginBean().getUser());
		old.setEditTime(new Date());
		old.setRemark(io.getRemark());
		inOrderDAO.update(old);
	}

	public InOrder findById(String id) throws CRUDException {
		return inOrderDAO.get(id);
	}

	public Page<InOrder> listPagination(Page<InOrder> page)
			throws CRUDException {
		Users user = LoginBean.getLoginBean().getUser();
		Set<RoleUsers> set = user.getRoleUsers();
		boolean flag = false;
		for (RoleUsers ru : set) {
			if (ru.getRole().getName().equals("库存管理员")) {
				flag = true;
			}
		}
		if (flag) {
			return inOrderDAO.pagination(page,
					"from InOrder where deleteFlag='0' and auditState=2", null);
		} else {
			if (user.getDepartment().getManager().getId().equals(user.getId())) {
				return inOrderDAO.pagination(page,
						"from InOrder where deleteFlag='0'", "createTime desc");
			} else {
				return inOrderDAO.pagination(page,
						"from InOrder where deleteFlag='0'"
								+ "  and creator.id= '" + user.getId() + "'",
						"createTime desc");
			}
		}
	}

	/**
	 * 批量删除商品
	 * 
	 * @param idList
	 * @return
	 * @throws CRUDException
	 */
	public void deleteInOrder(String id) throws CRUDException {
		InOrder io = inOrderDAO.get(id);
		io.setDeleteFlag("1");
		inOrderDAO.update(io);
	}
}